#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include<time.h>
#define PORT 8000


void process_conn_client(int s)
{
    ssize_t size = 0;
    char buffer[1024];

    for(;;){
        size = read(0, buffer, 1024);
        if(size>0){
            write(s,buffer, size);
            size =read(s, buffer, 1024);
            write(1, buffer, size);
        }
    }
}

int main(int argc, char *argv[])
{
    int s;
    struct sockaddr_in server_addr;
    time_t start_time,end_time;
  

    s = socket(AF_INET, SOCK_STREAM, 0);
    if(s<0){
        printf("socket error\n");
        return -1;
    }

    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(PORT);

    inet_pton(AF_INET, argv[1], &server_addr.sin_addr);
    start_time=time(NULL);
    if (connect(s, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) != 0)
    {
        end_time=time(NULL);
        printf("%f\n",difftime(end_time,start_time));
        printf("connect error: %s",strerror(errno));
        return -1;
    }
        
    process_conn_client(s);
    return 0;
}